package org.jeecg.modules.demo.timeline.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.timeline.service.IPatentTimelineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description: 专利时间脉络控制器
 * @Author: jeecg-boot
 * @Date: 2025-10-14
 * @Version: V1.0
 */
@Tag(name = "专利时间脉络")
@RestController
@RequestMapping("/timeline/patentTimeline")
@Slf4j
public class PatentTimelineController {

    @Autowired
    private IPatentTimelineService patentTimelineService;

    /**
     * 获取专利时间脉络数据
     *
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param eventType 事件类型
     * @param inventor 发明人
     * @param institution 申请机构
     * @return 时间脉络数据
     */
    @Operation(summary = "获取专利时间脉络数据")
    @GetMapping(value = "/getTimelineData")
    public Result<List<Map<String, Object>>> getTimelineData(
            @RequestParam(name = "startDate", required = false) String startDate,
            @RequestParam(name = "endDate", required = false) String endDate,
            @RequestParam(name = "eventType", required = false) String eventType,
            @RequestParam(name = "inventor", required = false) String inventor,
            @RequestParam(name = "institution", required = false) String institution) {
        try {
            log.info("获取专利时间脉络数据请求，开始日期: {}, 结束日期: {}, 事件类型: {}, 发明人: {}, 机构: {}", 
                    startDate, endDate, eventType, inventor, institution);
            List<Map<String, Object>> timelineData = patentTimelineService.getTimelineData(
                    startDate, endDate, eventType, inventor, institution);
            return Result.OK(timelineData);
        } catch (Exception e) {
            log.error("获取专利时间脉络数据失败", e);
            return Result.error("获取专利时间脉络数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取时间脉络统计数据
     *
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param eventType 事件类型
     * @param inventor 发明人
     * @param institution 申请机构
     * @return 统计数据
     */
    @Operation(summary = "获取时间脉络统计数据")
    @GetMapping(value = "/getStatistics")
    public Result<Map<String, Object>> getStatistics(
            @RequestParam(name = "startDate", required = false) String startDate,
            @RequestParam(name = "endDate", required = false) String endDate,
            @RequestParam(name = "eventType", required = false) String eventType,
            @RequestParam(name = "inventor", required = false) String inventor,
            @RequestParam(name = "institution", required = false) String institution) {
        try {
            log.info("获取时间脉络统计数据请求，开始日期: {}, 结束日期: {}", startDate, endDate);
            Map<String, Object> statistics = patentTimelineService.getTimelineStatistics(
                    startDate, endDate, eventType, inventor, institution);
            return Result.OK(statistics);
        } catch (Exception e) {
            log.error("获取时间脉络统计数据失败", e);
            return Result.error("获取时间脉络统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 生成时间脉络数据
     * 从专利表生成时间脉络数据
     *
     * @return 操作结果
     */
    @Operation(summary = "生成时间脉络数据")
    @PostMapping(value = "/generateTimeline")
    public Result<String> generateTimeline() {
        try {
            log.info("开始生成时间脉络数据");
            patentTimelineService.generateTimelineData();
            log.info("时间脉络数据生成完成");
            return Result.OK("时间脉络数据生成成功");
        } catch (Exception e) {
            log.error("生成时间脉络数据失败", e);
            return Result.error("生成时间脉络数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取年份分布统计
     *
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 年份分布
     */
    @Operation(summary = "获取年份分布统计")
    @GetMapping(value = "/getYearDistribution")
    public Result<List<Map<String, Object>>> getYearDistribution(
            @RequestParam(name = "startDate", required = false) String startDate,
            @RequestParam(name = "endDate", required = false) String endDate) {
        try {
            log.info("获取年份分布统计请求，开始日期: {}, 结束日期: {}", startDate, endDate);
            List<Map<String, Object>> yearDistribution = patentTimelineService.getYearDistribution(startDate, endDate);
            return Result.OK(yearDistribution);
        } catch (Exception e) {
            log.error("获取年份分布统计失败", e);
            return Result.error("获取年份分布统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取机构统计
     *
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 机构统计
     */
    @Operation(summary = "获取机构统计")
    @GetMapping(value = "/getInstitutionStats")
    public Result<List<Map<String, Object>>> getInstitutionStats(
            @RequestParam(name = "startDate", required = false) String startDate,
            @RequestParam(name = "endDate", required = false) String endDate) {
        try {
            log.info("获取机构统计请求，开始日期: {}, 结束日期: {}", startDate, endDate);
            List<Map<String, Object>> institutionStats = patentTimelineService.getInstitutionStats(startDate, endDate);
            return Result.OK(institutionStats);
        } catch (Exception e) {
            log.error("获取机构统计失败", e);
            return Result.error("获取机构统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取发明人统计
     *
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 发明人统计
     */
    @Operation(summary = "获取发明人统计")
    @GetMapping(value = "/getInventorStats")
    public Result<List<Map<String, Object>>> getInventorStats(
            @RequestParam(name = "startDate", required = false) String startDate,
            @RequestParam(name = "endDate", required = false) String endDate) {
        try {
            log.info("获取发明人统计请求，开始日期: {}, 结束日期: {}", startDate, endDate);
            List<Map<String, Object>> inventorStats = patentTimelineService.getInventorStats(startDate, endDate);
            return Result.OK(inventorStats);
        } catch (Exception e) {
            log.error("获取发明人统计失败", e);
            return Result.error("获取发明人统计失败: " + e.getMessage());
        }
    }
}

